package com.rui.study.com.rui.study.feign.hystrix;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;

import java.util.concurrent.atomic.AtomicInteger;

public class ProducerFactoryBean implements FactoryBean<Producer>, InitializingBean, ApplicationContextAware {

    private AtomicInteger num = new AtomicInteger();

    private ApplicationContext applicationContext;


    @Override
    public Producer getObject() throws Exception {
        Environment env = applicationContext.getEnvironment();
        String name = env.getProperty("foo");
        return new Producer(name + "_" + this.num.getAndIncrement());
    }

    @Override
    public Class<?> getObjectType() {
        return this.getClass();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("....afterPropertiesSet()....");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
